/**
 * 
 */
package com.hoperun.tags;

import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.hoperun.bean.AdminDictionaryType;
import com.hoperun.framework.query.Criteria;
import com.hoperun.utils.CodeMasterHelper;

public class CodeMasterTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String var;

	private String type;

	private Integer page;

	private Long status;

	private String sort_col;

	private Integer page_size;

	private String default_value;

	private Iterator<AdminDictionaryType> iterator = null;

	@Override
	public int doStartTag() throws JspException {

		Criteria criteria = new Criteria(AdminDictionaryType.TABLE_PREFIX);

		if (null != type) {
			criteria.addEqual(AdminDictionaryType.COL_DTYPE_KEY, type,
					Criteria.AND);
		}
		if (null != status) {
			criteria.addEqual(AdminDictionaryType.COL_STATUS, status,
					Criteria.AND);
		}
		if (StringUtils.isNotBlank(sort_col)) {
			criteria.orderBy(sort_col);
		}

		List<AdminDictionaryType> list = CodeMasterHelper
				.getCodeMaster(criteria);

		if (CollectionUtils.isNotEmpty(list)) {
			iterator = list.iterator();
		}

		if (null != iterator && iterator.hasNext()) {
			pageContext.setAttribute(var, iterator.next());
			return EVAL_BODY_INCLUDE;
		}

		return SKIP_BODY;

	}

	@Override
	public int doAfterBody() throws JspException {

		if (null != iterator && iterator.hasNext()) {
			pageContext.setAttribute(var, iterator.next());
			return EVAL_BODY_AGAIN;
		}
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {

		return super.doEndTag();
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @return the status
	 */
	public Long getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(Long status) {
		this.status = status;
	}

	/**
	 * @return the sort_col
	 */
	public String getSort_col() {
		return sort_col;
	}

	/**
	 * @param sort_col
	 *            the sort_col to set
	 */
	public void setSort_col(String sort_col) {
		this.sort_col = sort_col;
	}

	/**
	 * @return the page
	 */
	public Integer getPage() {
		return page;
	}

	/**
	 * @param page
	 *            the page to set
	 */
	public void setPage(Integer page) {
		this.page = page;
	}

	/**
	 * @return the page_size
	 */
	public Integer getPage_size() {
		return page_size;
	}

	/**
	 * @param page_size
	 *            the page_size to set
	 */
	public void setPage_size(Integer page_size) {
		this.page_size = page_size;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	public String getDefault_value() {
		return default_value;
	}

	public void setDefault_value(String default_value) {
		this.default_value = default_value;
	}

}
